package pers.vic.practice.module.system.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import pers.vic.boot.base.controller.BaseController;

import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.model.BaseSelectModel;
import pers.vic.boot.base.vo.Select2VO;
import pers.vic.practice.module.system.model.SysDictType;
import pers.vic.practice.module.system.service.SysDictService;
import pers.vic.practice.module.system.model.SysDict;
import pers.vic.practice.module.system.service.SysDictTypeService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:字典表 控制层
 * @author Vic.xu
 * @date: 2022-04-12 09:48
 */
@RestController
@RequestMapping("/system/dict")
public class SysDictController extends BaseController<SysDictService, SysDict>{

    @Resource
    private SysDictTypeService sysDictTypeService;


    @GetMapping("/home")
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("system/dict/home");
        return modelAndView;
    }

    @GetMapping("/form")
    public ModelAndView form(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        //全部父类型
        modelAndView.addObject("types", sysDictTypeService.list(new SysDictType()));
        if (id != null) {
            SysDict entity = service.findById(id);
            modelAndView.addObject("entity", entity);
        }
        modelAndView.setViewName("system/dict/form");
        return modelAndView;
    }


    @RequestMapping("/byTypeCode")
    public BaseResponse<?> byTypeCode(String pid) {
        List<SysDict> list = service.findByTypeCode(pid);
        List<BaseSelectModel> voList = list.stream().map(d -> {
            return new BaseSelectModel(d.getValue(), d.getText());
        }).collect(Collectors.toList());
        return BaseResponse.success(voList);
    }

    @RequestMapping("/byParentValue")
    public BaseResponse<?> byPid(@RequestParam("pid") String parentValue, String parentTypeCode) {
        List<SysDict> list = service.findByParentValueAndCode(parentValue, parentTypeCode);
        List<BaseSelectModel> voList = list.stream().map(d -> {
            return new BaseSelectModel(d.getValue(), d.getText());

        }).collect(Collectors.toList());
        return BaseResponse.success(voList);
    }


}
